package com.bluedon.ism.commons.dao;


import com.bluedon.ism.commons.entity.Menu;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("menuDAO")
public class MenuDAO extends BaseDAO {
	
	private static final Log log = LogFactory.getLog(MenuDAO.class);
	
	@SuppressWarnings({ "unchecked" })
	private List<Menu> getMenuList(int parentId, int type, int isLeaf) {
		StringBuffer hql = new StringBuffer("");
		hql.append("from Menu m where 1 = 1 ");
		if (parentId > -1) {
			hql.append(" and m.parentId = " + parentId);
		}
		if (type > -1) {
			hql.append(" and m.types = " + type);
		}
		if (isLeaf > -1) {
			hql.append(" and m.isLeaf = " + isLeaf);
		}
		hql.append(" order by m.mid ");
		return getHQLList(hql.toString(), -1, -1);
	}
	

	
	public Menu findMenuById(long id) {
		return (Menu) findObjectById(Menu.class, id);
	}
	

	public List<Menu> findAll(){
		String hql ="from Menu";
		return getHibernateTemplate().find(hql);
	}

	public List<Menu> findMenuByPcAccount(long aid){
	    StringBuffer hql =new StringBuffer("from Menu m where m.types = 0");
	    if (aid > -1) {
	        hql.append(" and m.mid in (" +
                        " select p.menu.mid from Permission p where p.permType = 0 and p.pid in ( " +
                        " select rp.permission.pid from AccountRole ar ,RolePermission rp where ar.role.rid = rp.role.rid " +
                        " and ar.account.aid =  " + aid +
                        " )  " +
                        ")  ");
	    };
	    hql.append(" order by m.mid");
	    return getHibernateTemplate().find(hql.toString());
	}
	
}
